<template>
  <div>
    <el-card class="box-card ">
      <div class="content">

        <el-row :gutter="8"
                type="flex"
                align="middle"
                style="margin-bottom:10px;flex-wrap:wrap;align-items: between;">
          <el-col :span="1.5">
            字典名称
          </el-col>
          <el-col :span="40">
            <el-select placeholder="请选择"
                       size="small"
                       v-model="queryParams.dictType">

              <el-option v-for="el in userinfoList"
                         :key="el.dictId"
                         :label="el.dictName"
                         :value="el.dictType">
              </el-option>

            </el-select>

          </el-col>
          <el-col :span="1.5">
            字典标签
          </el-col>
          <el-col :span="40">
            <el-input size="small"
                      v-model="queryParams.dictLabel"
                      style="width: 200px"
                      placeholder="请输入字典类型"></el-input>
          </el-col>

          <el-col :span="1.5">
            状态
          </el-col>
          <el-col :span="40">
            <el-select placeholder="请选择"
                       size="small"
                       v-model="queryParams.status">

              <el-option label="正常"
                         :value="0">
              </el-option>
              <el-option label="停用"
                         :value="1">
              </el-option>

            </el-select>

          </el-col>

          <el-col :span="1.5">
            <el-button size="mini"
                       type="primary"
                       icon="el-icon-search"
                       @click="getData()">搜索</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button icon='el-icon-refresh'
                       size="mini"
                       @click="refreshTable()">重置</el-button>
          </el-col>
        </el-row>
        <div class="btnList">
          <el-row :gutter="8"
                  type="flex"
                  align="middle"
                  class="btn_nav">
            <el-col :span="1.5">
              <el-button size="mini"
                         type="primary"
                         icon="el-icon-plus"
                         plain
                         @click="addXqq">新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button size="mini"
                         type="success"
                         icon="el-icon-edit"
                         plain
                         @click="updatadata"
                         :disabled='checkItem.length!=1'>修改</el-button>
            </el-col>

            <el-col :span="1.5">
              <el-button size="mini"
                         type="danger"
                         icon="el-icon-delete"
                         plain
                         :disabled='checkItem.length==0'
                         @click="delMore">删除</el-button>
            </el-col>

            <el-col :span="1.5">
              <el-button size="mini"
                         type="warning"
                         icon="el-icon-download"
                         @click="exportdata"
                         plain>导出</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button size="mini"
                         type="danger"
                         icon="el-icon-download"
                         @click="clear"
                         plain>清理缓存</el-button>
            </el-col>
          </el-row>

          <el-row :gutter="8"
                  type="flex"
                  align="middle"
                  class="btn_nav">

            <el-col :span="1.5">
              <el-button size="mini"
                         icon="el-icon-search"
                         circle></el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button @click="refreshTable"
                         size="mini"
                         icon="el-icon-refresh"
                         circle></el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button @click="refreshTable"
                         size="mini"
                         icon="el-icon-menu"
                         circle></el-button>
            </el-col>
          </el-row>
        </div>
        <el-table :data="tableData"
                  v-loading="loading"
                  style="width: 100%"
                  :header-cell-style="{background:'#f8f8f9','text-align':'center'}"
                  @selection-change="handleSelectionChange"
                  :cell-style="{'text-align':'center'}">
          <el-table-column width="80"
                           type="selection">
          </el-table-column>
          <el-table-column prop="dictCode"
                           width="80"
                           label="字典编码">
          </el-table-column>
          <el-table-column prop="dictLabel"
                           width="80"
                           label="字典标签">
          </el-table-column>
          <el-table-column prop="dictValue"
                           width="150"
                           label="字典键值">

          </el-table-column>

          <el-table-column prop="dictSort"
                           width="80"
                           label="字典排序">

          </el-table-column>
          <el-table-column prop="status"
                           width="80"
                           label="状态">
            <template slot-scope="scope">
              <span>{{scope.row.status==0?'正常':'停用'}}</span>
            </template>
          </el-table-column>
          <el-table-column prop="remark"
                           label="备注">
          </el-table-column>

          <el-table-column prop="createTime"
                           label="创建时间">
          </el-table-column>

          <el-table-column label="操作">
            <template v-slot="data">
              <el-button icon="el-icon-edit"
                         type="text"
                         @click="updateHandler(data.row.dictCode)">修改</el-button>
              <el-button v-if="data.row.userId!=1"
                         icon="el-icon-delete"
                         type="text"
                         @click="deleteHandler(data.row.dictCode)">删除</el-button>
            </template>

          </el-table-column>

        </el-table>
        <div class="page"
             v-show="total!=0">
          <el-pagination background
                         @size-change="handleSizeChange"
                         @current-change="handleCurrentChange"
                         :current-page="queryParams.pageNum"
                         :page-sizes="[3, 5, 9, 10]"
                         :page-size="queryParams.pageSize"
                         layout="total, sizes, prev, pager, next, jumper"
                         :total="total">
          </el-pagination>
        </div>

      </div>
    </el-card>
    <div>
      <el-dialog :title="flag?'添加字典数据':'修改字典数据'"
                 :visible.sync="dialogVisible"
                 width="40%">
        <el-form :model="formData"
                 ref="ruleForm"
                 label-width="80px">

          <el-form-item label="字典类型"
                        prop="postCode">
            <el-input v-model="formData.dictType"
                      disabled
                      placeholder="字典类型"></el-input>
          </el-form-item>
          <el-form-item label="数据标签"
                        prop="postCode">
            <el-input v-model="formData.dictLabel"
                      placeholder="数据标签"></el-input>
          </el-form-item>
          <el-form-item label="数据键值"
                        prop="postCode">
            <el-input v-model="formData.dictValue"
                      placeholder="数据键值"></el-input>
          </el-form-item>
          <el-form-item prop="postSort"
                        label="岗位顺序">
            <el-input-number v-model="formData.dictSort"
                             controls-position="right"
                             :min="0"
                             :max="10000000"></el-input-number>
          </el-form-item>
          <el-form-item label="状态">
            <template>
              <el-radio v-model="formData.status"
                        label="0">正常</el-radio>
              <el-radio v-model="formData.status"
                        label="1">停用</el-radio>
            </template>

          </el-form-item>
          <el-form-item label="备注">
            <el-input type="textarea"
                      v-model="formData.remark"
                      placeholder="备注"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer"
              class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary"
                     @click="update">确 定</el-button>
        </span>
      </el-dialog>
    </div>

  </div>
</template>

<script>
export default {
  data() {
    return {
      // 权限

      queryParams: {
        pageNum: 1,
        pageSize: 5,
        dictLabel: null,
        dictType: null,
        status: null
      },
      // 展开的数组

      formData: {
        createBy: null,
        createTime: null,
        cssClass: null,
        default: false,
        dictCode: null,
        dictLabel: null,
        dictSort: 0,
        dictType: null,
        dictValue: null,
        isDefault: null,
        listClass: null,
        params: {},
        remark: null,
        searchValue: null,
        status: '0',
        updateBy: null,
        updateTime: null
      },
      tableData: [],
      checkItem: [],
      seltime: [],
      total: 0,
      loading: false,
      dialogVisible: false,
      flag: true,
      userinfoList: []
    }
  },
  mounted() {
    this.getseleData()
  },
  methods: {
    // 获取下拉框数据
    async getData() {
      this.loading = true
      let { data } = await this.$axios.get('system/dict/data/list', { params: this.queryParams })
      this.tableData = data.rows
      this.total = data.total
      this.loading = false
    },
    async getseleData() {
      let { data } = await this.$axios.get(`system/dict/type/${this.$route.query.dictId}`)
      let { data: res } = await this.$axios.get('system/dict/type/list')
      this.userinfoList = res.rows
      this.queryParams.dictType = data.data.dictType
      this.getData()
    },

    async clear() {
      let { data } = await this.$axios.delete('system/dict/type/clearCache')
      if (data.code != 200) return this.$message.error(data.msg)
      this.$message.success(data.msg)
      this.getData()
    },

    updatadata() {
      this.updateHandler(this.checkItem[0].dictCode)
    },
    async updateHandler(id) {
      this.dialogVisible = true
      this.flag = false
      let { data } = await this.$axios.get(`system/dict/data/${id}`)
      this.formData = data.data
      // this.menuIds = res.checkedKeys
    },
    delMore() {
      let ids = this.checkItem.map((el) => el.dictCode)
      this.deleteHandler(ids)
    },
    deleteHandler(id) {
      this.$confirm(`是否确认删除编号为${id.toString()}的数据项?`, '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          let data = await this.$axios.delete(`system/dict/data/${id.toString()}`)
          console.log(data)
          if (data.data.code != 200) return this.$message.error(data.data.msg)
          this.$message({
            type: 'success',
            message: data.data.msg
          })
          this.getData()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },

    async update() {
      let data = null
      this.dialogVisible = false
      if (this.formData.dictCode) {
        data = await this.$axios.put('system/dict/data', this.formData)
      } else {
        data = await this.$axios.post('system/dict/data', this.formData)
      }
      if (data.data.code != 200) return this.$message.error(data.data.msg)
      this.$message({ type: 'success', message: data.data.msg })
      this.getData()
    },

    async addXqq() {
      this.dialogVisible = true
      this.flag = true
      this.formData = {
        createBy: null,
        createTime: null,
        cssClass: null,
        default: false,
        dictCode: null,
        dictLabel: null,
        dictSort: 0,
        dictType: this.queryParams.dictType,
        dictValue: null,
        isDefault: null,
        listClass: null,
        params: {},
        remark: null,
        searchValue: null,
        status: '0',
        updateBy: null,
        updateTime: null
      }
    },

    // 刷新
    // 刷新
    refreshTable() {
      this.loading = true
      this.getData()
      this.queryParams = { pageNum: 1, pageSize: 5, dictLabel: null, status: null }
    },

    handleSizeChange(val) {
      this.queryParams.pageSize = val
      this.getData()
    },
    handleCurrentChange(val) {
      this.queryParams.pageNum = val
      this.getData()
    },
    handleSelectionChange(val) {
      this.checkItem = val
    },
    async exportdata() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          // let data = await this.$axios.get('system/user/export', { params: this.queryParams })
          // console.log(data)
          this.getExport()
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })
    },
    async getExport() {
      let { data } = await this.$axios.get('system/dict/data/export', { params: this.queryParams })

      if (data.code != 200) {
        this.$message.error('失败')
      } else {
        window.location.href = 'https://jumeng.xzjumeng.com/prod-api/common/download?fileName=' + encodeURI(data.msg) + '&delete=' + true
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.btnList {
  margin-bottom: 8px;
  margin-top: 15px;
  display: flex;
  justify-content: space-between;
}
.page {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}
</style>